/*
 * h_cc2640r2f_uart.c
 *
 *  Created on: Sep 19, 2019
 *      Author: hemingway
 */
#include "h_cc2640r2f_uart.h"
#include <ti/drivers/uart/UARTCC26XX.h>

#include "h_cc2640r2f_define.h"
#include <stdio.h>
#include "s_opendoor_ctl.h"

#include "s_uart_msg.h"

#define H_CC2640R2F_UART_OK                     0
#define H_CC2640R2F_UART_Open_Failed            (-1)
#define H_CC2640R2F_UART_Sema_Failed            (-2)

#define H_CC2640R2F_UART_BaudRate               115200

#define H_CC2640R2F_UARTRXMAXLENGTH             256//H_CC2640R2F_UART_RxLength


unsigned char H_CC2640R2F_UARTRxBuffer[H_CC2640R2F_UARTRXMAXLENGTH];
volatile unsigned short H_CC2640R2F_UARTRxBufferLength = 0U;


static UART_Handle H_CC2640R2F_UARTHandle = NULL;
static SemaphoreP_Handle h_cc2640r2f_semaphoreP_handle = NULL;

static void H_CC2640R2F_UART_RxCallback(UART_Handle handle, void *buf, size_t count)
{
    H_CC2640R2F_UARTRxBufferLength = count;

   // printf("The length is %d\r\n", count);
    S_Uart_Msg_Handle(H_CC2640R2F_UARTRxBuffer, H_CC2640R2F_UARTRxBufferLength);


   // S_OpenDoor_SendCheckSignedData(H_CC2640R2F_UARTRxBuffer, H_CC2640R2F_UARTRxBufferLength);

//    H_CC2640R2F_UART_RxEnable();
}

signed char H_CC2640R2F_UART_Init(void)
{
    UART_Params uartParams;
    UART_init();

    UART_Params_init(&uartParams);
    uartParams.baudRate  = H_CC2640R2F_UART_BaudRate;
    uartParams.writeMode = UART_MODE_BLOCKING;

    uartParams.readMode = UART_MODE_CALLBACK;

    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.readCallback = H_CC2640R2F_UART_RxCallback;

//    uartParams.readReturnMode = UART_RETURN_NEWLINE;
//    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.dataLength = UART_LEN_8;
    uartParams.stopBits = UART_STOP_ONE;//UART_STOP_TWO;//UART_STOP_ONE;
    uartParams.parityType = UART_PAR_NONE;//UART_PAR_EVEN;//UART_PAR_NONE;

    h_cc2640r2f_semaphoreP_handle = SemaphoreP_createBinary(1);
    if (NULL == h_cc2640r2f_semaphoreP_handle)
    {
        return H_CC2640R2F_UART_Sema_Failed;
    }


    H_CC2640R2F_UARTHandle = UART_open(0, &uartParams);
    if (NULL == H_CC2640R2F_UARTHandle)
    {
        return H_CC2640R2F_UART_Open_Failed;
    }

//    /* Disable RX in case that is needed to save power */
//    UART_control(H_CC2640R2F_UARTHandle, UART_CMD_RXDISABLE, NULL);
//    UART_control(H_CC2640R2F_UARTHandle, UART_CMD_RXENABLE, NULL);


    /* RX data even not a new line mode */
    UART_control(H_CC2640R2F_UARTHandle, \
                 UARTCC26XX_RETURN_PARTIAL_ENABLE, NULL);


//    H_CC2640R2F_UART_RxEnable();

    return H_CC2640R2F_UART_OK;
}

void H_CC2640R2F_UART_TxBuff(const unsigned char *data, \
                                unsigned short length)
{
    if(SemaphoreP_pend(h_cc2640r2f_semaphoreP_handle, \
                       SemaphoreP_WAIT_FOREVER) == \
                                            SemaphoreP_OK)
    {
        UART_write(H_CC2640R2F_UARTHandle, data, length);
        SemaphoreP_post(h_cc2640r2f_semaphoreP_handle);
    }
//    UART_write(H_CC2640R2F_UARTHandle, data, length);

}

void H_CC2640R2F_UART_RxEnable(void)
{
        UART_read(H_CC2640R2F_UARTHandle, H_CC2640R2F_UARTRxBuffer, \
                  H_CC2640R2F_UARTRXMAXLENGTH);
}

//void H_CC2640R2F_UART_printf(const char* format, ...)
//{
//    va_list arg;
//    va_start(arg, format);
//    unsigned char buf[128];
//    unsigned short len;
//    len = vsprintf((char*)buf, format, arg);
//    H_CC2640R2F_UART_TxBuff(buf, len);
//}




